﻿using System;
using System.Collections.Generic;
using System.Linq;

using Preactor.ObjectModel;

namespace Preactor.ObjectModelExtensions.Entities
{
    /// <summary>
    /// OptionPicker will link to a TDF format and faciliates the creation of an edit window to allow the user
    /// to pick on option.
    /// 
    /// For this to work you will need to create TWO formats:
    /// MyFormat,,0,1:
    ///	    Number,0,INTEGER,
    ///		    PRIMARY KEY
    ///		    HIDDEN:
    ///	    Option,"",STRING,
    ///	        SUBSTITUTE(0 -> "Please select")
    ///		    DATABASE(MyFormat Options(Name)):
    ///	    Import Marker,-1,INTEGER,
    ///		    DIALOG LEVEL 99
    ///		    DIALOG ONLY
    ///		    READ ONLY
    ///		    IMPORT MARKER;
    ///;
    ///;
    /// MyFormat Options,,0,100:
    ///	    Number,0,INTEGER,
    ///		    PRIMARY KEY
    ///		    HIDDEN:
    ///	    Name,"",STRING,
    ///		    UNIQUE
    ///		    FREE FORMAT(40):
    ///	    Import Marker,-1,INTEGER,
    ///		    DIALOG LEVEL 99
    ///		    DIALOG ONLY
    ///		    READ ONLY
    ///		    IMPORT MARKER;
    ///;
    /// </summary>
    public class OptionPicker
    {
        private IEnumerable<string> Options { get; set; }

        private PreactorObjectModel Pom { get; set; }

        private string PickerFormat { get; set; }
        private string PickerOptionsFormat { get; set; }

        public OptionPicker(PreactorObjectModel pom, string pickerFormat, string pickerOptionsFormat, IEnumerable<string> options )
        {
            Pom = pom;
            PickerFormat = pickerFormat;
            PickerOptionsFormat = pickerOptionsFormat;
            Options = options;
            PopulateOptionsFormat();
        }

        private void PopulateOptionsFormat()
        {
            Pom.Preactor.Clear(PickerOptionsFormat);

            foreach (var option in Options)
            {
                int recordNumber = Pom.Preactor.CreateRecord(PickerOptionsFormat);
                Pom.Preactor.WriteField(PickerOptionsFormat, "Name", recordNumber, option);
            }
        }

        public string GetSelectedOption()
        {
            // Check if PickerFormat has a record otherwise create one.
            if( Pom.Preactor.RecordCount( PickerFormat ) == 0)
            {
                Pom.Preactor.CreateRecord(PickerFormat);
            }

            // Display editor form
            EditRecordResult ERR = Pom.Preactor.EditRecord(PickerFormat, 1);

            if (ERR.ToString() == "OK")
            {
                string selectedScheduleRange = Pom.Preactor.ReadFieldString(PickerFormat, "Option", 1);

                if (!Options.Contains(selectedScheduleRange))
                {
                    throw new ArgumentException("The selected item does not exist in the list.");
                }

                return selectedScheduleRange;
            }

            return string.Empty;
        }
    }
}
